import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/nodeDecision'

const api = {

    /**
     * 新增节点流转规则
     * @param nodeDecision
     * @returns {Promise<*|undefined>}
     */
    createNodeDecision: (nodeDecision) =>
      axiosClient().post(`${requestUrl}/addInfo`, nodeDecision),

    /**
     * 根据ID删除节点流转规则
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteNodeDecisionById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新节点流转规则
     * @param id
     * @param nodeDecision
     * @returns {Promise<*|undefined>}
     */
    updateNodeDecisionFull: (id, nodeDecision) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, nodeDecision),

    /**
     * 根据ID部分更新节点流转规则
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateNodeDecisionPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询节点流转规则
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getNodeDecisionById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询节点流转规则
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNodeDecisionPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询节点流转规则
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNodeDecisionList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.processId) {
            params.append('processId', businessParam.processId)
        }
        if (businessParam.currentNodeId) {
            params.append('currentNodeId', businessParam.currentNodeId)
        }
        if (businessParam.decisionName) {
            params.append('decisionName', businessParam.decisionName)
        }
        if (businessParam.decisionType) {
            params.append('decisionType', businessParam.decisionType)
        }
        if (businessParam.priority) {
            params.append('priority', businessParam.priority)
        }
        if (businessParam.prevNodeId) {
            params.append('prevNodeId', businessParam.prevNodeId)
        }
        if (businessParam.nextNodeId) {
            params.append('nextNodeId', businessParam.nextNodeId)
        }
        if (businessParam.conditionExpr) {
            params.append('conditionExpr', businessParam.conditionExpr)
        }
        if (businessParam.defaultOpinion) {
            params.append('defaultOpinion', businessParam.defaultOpinion)
        }
        if (businessParam.submitFunc) {
            params.append('submitFunc', businessParam.submitFunc)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
